package com.aiju.cash.ui.account.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.cash.R;
import com.aiju.cash.ui.account.entity.ItemList;
import com.aiju.cash.ui.account.manager.AccountTypeManager;
import com.aiju.utils.DateUtil;
import com.aiju.utils.Util;

import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-04-08 15:52
 * @version: V1.0
 * @Description: 日期下的账目
 */
public class CalenderAdatper extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ItemList.Item> items;

    public CalenderAdatper(List<ItemList.Item> items) {
        this.items = items;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_account_info, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        final ItemList.Item item = items.get(position);
        MyViewHolder holder=(MyViewHolder)viewHolder;
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(item);
                }
            }
        });

        holder.timeTextView.setText(DateUtil.getStringByFormat(item.getModifyDate(), DateUtil.dateFormatHM));
        holder.userNameTextView.setText(item.getOperatorName());
        holder.picImageView.setImageResource(AccountTypeManager.getPic(item.getCategoryName()));
        holder.nameTextView.setText(item.getCategoryName());
        holder.remarkTextView.setText(item.getRemark());
        holder.moneyTextView.setText(item.getMoney());
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    public boolean isEmpty() {
        return Util.isEmpty(items);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        View rootView;

        TextView timeTextView;
        TextView userNameTextView;
        ImageView picImageView;

        TextView nameTextView;
        TextView remarkTextView;
        TextView moneyTextView;

        public MyViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
            timeTextView = (TextView) itemView.findViewById(R.id.tv_account_time);
            userNameTextView = (TextView) itemView.findViewById(R.id.tv_account_user_name);
            picImageView = (ImageView) itemView.findViewById(R.id.iv_account_pic);

            nameTextView = (TextView) itemView.findViewById(R.id.tv_account_name);
            remarkTextView = (TextView) itemView.findViewById(R.id.tv_account_remark);
            moneyTextView = (TextView) itemView.findViewById(R.id.tv_account_money);
        }
    }

    //点击事件
    private AccountInfoAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(AccountInfoAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(ItemList.Item item);
    }
}
